package com.yzt.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yzt.shiro.ShiroRealm;
import com.yzt.user.entity.Info;
import com.yzt.user.service.IInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Y
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/user/info")
@Api(tags = "用户信息统一接口")
public class InfoController {

	@Autowired
	private IInfoService infoService;

	@Autowired
	private ShiroRealm shiroRealm;

	@GetMapping("list")
	@ApiOperation("用户列表")
	public List<Info> list(HttpServletRequest request) {
		return infoService.list();
	}

	@PostMapping("list")
	@ApiOperation("带缓存的用户列表")
	@RequiresPermissions(value = "tttt")
	public List<Info> cachelist(HttpServletRequest request) {
		return infoService.getAllUserInfo();
	}

	@RequestMapping(value = "login", method = { RequestMethod.POST, RequestMethod.GET })
	@ApiOperation("登录接口")
	public String user_login() {
		UsernamePasswordToken token = new UsernamePasswordToken("root", "root".toCharArray());
		Subject subject = SecurityUtils.getSubject();
		subject.login(token);
		return "SUCCESS";
	}

	@PostMapping("clearShiroCache")
	@ApiOperation("清空shiro权限缓存")
	public String clear_shiro_cache() {
		shiroRealm.clearAllCache();
		return "SUCCESS";
	}
}
